package com.ys.malini.web.action;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.DispatchAction;

import com.ys.malini.dao.ICompanyDao;
import com.ys.malini.model.TCompanyinfo;
import com.ys.malini.web.form.CompanyInfoForm;

/*
 * 公司信息管理
 * 
 */
public class CompanyAction extends DispatchAction {

	private ICompanyDao companyDao;


	/**
	 * 跳到查询页面
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	public ActionForward toQuery(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		List<TCompanyinfo> complist = companyDao.query(null,true);
		request.setAttribute("complist", complist);
		return mapping.findForward("listPage");
	}


	/**
	 * 公司信息查询
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	public ActionForward query(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		CompanyInfoForm formbean = (CompanyInfoForm)form;
		List<TCompanyinfo> complist = companyDao.query(formbean,true);
		request.setAttribute("complist", complist);
		return mapping.findForward("listPage");
	}
	
	
	/**
	 * 公司信息查询
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	public ActionForward queryQt(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		String compType = request.getParameter("compType");
		TCompanyinfo compinfo = companyDao.queryTop(compType);
		request.setAttribute("compinfo", compinfo);
		if("0".equals(compType)){
			return mapping.findForward("aboutus");
		}else{
			return mapping.findForward("contact");
		}
		
	}
	
	

	/**
	 * 跳到更新页面
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	public ActionForward toUpdate(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		String compId = request.getParameter("compId");
		TCompanyinfo compinfo = companyDao.queryCompanyInfoById(compId);
		request.setAttribute("compinfo", compinfo);
		return mapping.findForward("update");
	}

	/**
	 * 更新页面
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	public ActionForward update(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		CompanyInfoForm formbean =(CompanyInfoForm)form;
		String message = companyDao.updateCompanyInfoById(formbean);
		request.setAttribute("message",message);
		return mapping.findForward("update");
	}

	/**
	 * 跳到添加页面
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	public ActionForward toAdd(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		return mapping.findForward("add");
	}

	/**
	 * 公司信息更新
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	public ActionForward add(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		CompanyInfoForm formbean = (CompanyInfoForm) form;
		String message = companyDao.addCompanyInfo(formbean);
		request.setAttribute("message", message);
		return mapping.findForward("add");
	}
	
	
	/**
	 * 公司信息删除
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	public ActionForward delete(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		String compId=request.getParameter("compId");
		String compType=request.getParameter("compType");
		Map map = new HashMap();
		map.put("compId", compId);
		map.put("compType", compType);
		String message = companyDao.deleteCompanyInfo(map);
		request.setAttribute("message", message);
		List<TCompanyinfo> complist = companyDao.query(null,true);
		request.setAttribute("complist", complist);
		return mapping.findForward("listPage");
	}

	public ICompanyDao getCompanyDao() {
		return companyDao;
	}

	public void setCompanyDao(ICompanyDao companyDao) {
		this.companyDao = companyDao;
	}

	


	
}
